package com.hectopascal.utils;

import com.hectopascal.auth.bean.AdminUserDetails;
import com.hectopascal.pojo.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class CurrentUser {
    public static User getUserInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication == null || !authentication.isAuthenticated()){
            return null;
        }
        Object principal = authentication.getPrincipal();
        if(principal instanceof AdminUserDetails){
            return ((AdminUserDetails) principal).getUser();
        }
        return null;
    }

    public static String getUsername(){
        User user = getUserInfo();
        if(user != null){
            return user.getUsername();
        }
        return null;
    }

    public static boolean isAuthenticated() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication != null && authentication.isAuthenticated();
    }
}
